/**
 * 公司：金丹科技
 * 文件：EmployeeController
 * 作者：kong
 * 时间：2019/4/16 15:38
 * 描述：
 */

package com.springboot.test.controller;

import com.springboot.test.dao.DepartmentDao;
import com.springboot.test.dao.EmployeeDao;
import com.springboot.test.entities.Department;
import com.springboot.test.entities.Employee;
import com.springboot.test.exception.UserNotExistException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

@Controller
public class EmployeeController {
    @Autowired
    EmployeeDao employeeDao;


    @Autowired
    DepartmentDao departmentDao;
//多有员工信息


    @GetMapping("/emps")
    public String list(Model model){
        Collection<Employee> all = employeeDao.getAll();
        model.addAttribute("emps",all);
        return "emp/list";
    }

//用户添加页面
    @GetMapping("/emp")
    public String toAddPage(Model model){
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("depart",departments);
        return  "emp/add";
    }

    @PostMapping("/addemp")
    public String addemp(Employee employee){
        employeeDao.save(employee);
        System.out.println("保存成功"+employee);
        return  "redirect:/emps";
    }


    @GetMapping("/emp/{id}")
    public  String toupdate(@PathVariable("id") Integer id,Model model){
        Employee employee = employeeDao.get(id);
        model.addAttribute("emp",employee);

        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("depart",departments);
        return "emp/add";
    }
    @PutMapping("/addemp")
    public String toupdate(Employee employee){
        System.out.println("修改的员工数据信息"+employee);
        employeeDao.save(employee);
      return  "redirect:/emps";
    }
    @DeleteMapping("/addemp/{id}")
    public String todelete(@PathVariable("id") Integer id){
        employeeDao.delete(id);
        return  "redirect:/emps";
    }



}
